package org.example.quartz.controller;

import cn.hutool.core.date.StopWatch;
import cn.hutool.extra.spring.SpringUtil;
import org.example.quartz.service.LogService;
import org.example.quartz.util.Result;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.example.quartz.util.DataSourceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.time.LocalDateTime;
import java.util.Map;

@Slf4j
@RequestMapping(path = "/GSYY/ImpactData")
/**
 * 影像数据业务类
 */
public class ImpactDataController {
    Map<String, Object> gsMap = DataSourceUtil.getGS();
    //全局接口配置
    String IP = (String) gsMap.get("IP");
    String PORT = (String) gsMap.get("PORT");
    String ECC = (String) gsMap.get("ECC");
    String Bearer = (String) gsMap.get("Bearer");
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private LogService loginfo;

    /**
     * (一)文件影像描述信息接口
     */

    @PostMapping("/describe")
    public Result describe() {
        String methodName = "文件影像描述信息接口";
        //获取报文，后续需要手动报文拼接
        String jsonOne = jsonOne();
        //调用GS接口，推送给GS浪潮，获取返回值;
        StopWatch st = new StopWatch();
        st.start();
        String gsUrl = IP + ":" + PORT + "/api/jg/syncbddata/v1.0/bdsyncinterface/syncWldwList";
        String returnMSG = HttpRequest.post(gsUrl)
                .header("Content-Type", "application/json")
                .header("Accept-Language", "zh-CHS")
                .header("X-ECC-Current-Tenant", ECC)
                .header("Authorization", Bearer).timeout(200000)
                .body(jsonOne).execute().body();
        st.stop();
        System.out.println("结束时间：" + LocalDateTime.now());
        System.out.println("接口耗时：" + st.getTotalTimeMillis() + "ms");
        System.out.println("接口返回值：" + returnMSG);
        if (!returnMSG.isEmpty()) {
            JSONObject returnObj = JSONObject.parseObject(returnMSG);
            if (returnObj.get("code").equals("0")) {//成功
                //写入日志表
                int loginsert = loginfo.loginfo(jdbcTemplate, methodName, LocalDateTime.now().toString(), "成功", jsonOne, returnMSG, "", "", gsUrl, String.valueOf(LocalDateTime.now()));
                if (loginsert > 0) {
                    //System.out.println("日志写入成功！！");
                } else {
                    System.out.println("日志写入失败！！");
                }
            } else if (returnObj.get("code").equals("1")) { //失败
                jsonOne = jsonOne.replace("'", "\\\\''");
                returnMSG = returnMSG.replace("'", "\\\\''");
                //写入日志表
                int loginsert = loginfo.loginfo(jdbcTemplate, methodName, LocalDateTime.now().toString(), "失败", jsonOne, returnMSG, "", "", gsUrl, String.valueOf(LocalDateTime.now()));
                if (loginsert > 0) {
                    //System.out.println("日志写入成功！！");
                } else {
                    System.out.println("日志写入失败！！");
                }
            } else if (returnObj.get("code") == null && ("").equals(returnObj.get("code"))) {
                jsonOne = jsonOne.replace("'", "\\\\''");
                returnMSG = returnMSG.replace("'", "\\\\''");
                //写入日志表
                int loginsert = loginfo.loginfo(jdbcTemplate, methodName, LocalDateTime.now().toString(), "数据返回异常", jsonOne, returnMSG, "", "", gsUrl, String.valueOf(LocalDateTime.now()));
                if (loginsert > 0) {
                    //System.out.println("日志写入成功！！");
                } else {
                    System.out.println("日志写入失败！！");
                }
            }
        }
        return Result.success(returnMSG);
    }

    /**
     * (二)附件传输
     */

    @PostMapping("/fileTransfer")
    public Result fileTransfer() {
        String methodName = "附件传输接口";
        //获取报文，后续需要查询sql然后拼接报文手动报文拼接
        String jsonOne = "{\n" +
                "\t\"ORGCODE\": \"1001\",\n" +
                "\t\"BILLID\": [\"单据UUID\", \"123123\"]\n" +
                "\t\"ZIPCODE\": \"10012024120101\",\n" +
                "\t\"PUSHSTARTTIME\": \"2023-01-01\",\n" +
                "\t\"PUSHENDTIME\": \"2024-01-01\"\n" +
                "}";
        //调用GS接口，推送给GS浪潮，获取返回值;
        StopWatch st = new StopWatch();
        st.start();
        String gsUrl = IP + ":" + PORT + "/api/jg/syncbddata/v1.0/bdsyncinterface/syncWldwList";
        String returnMSG = HttpRequest.post(gsUrl)
                .header("Content-Type", "application/json")
                .header("Accept-Language", "zh-CHS")
                .header("X-ECC-Current-Tenant", ECC)
                .header("Authorization", Bearer).timeout(200000)
                .body(jsonOne).execute().body();
        st.stop();
        System.out.println("结束时间：" + LocalDateTime.now());
        System.out.println("接口耗时：" + st.getTotalTimeMillis() + "ms");
        System.out.println("接口返回值：" + returnMSG);
        if (!returnMSG.isEmpty()) {
            JSONObject returnObj = JSONObject.parseObject(returnMSG);
            if (returnObj.get("code").equals("0")) {//成功
                //写入日志表
                int loginsert = loginfo.loginfo(jdbcTemplate, methodName, LocalDateTime.now().toString(), "成功", jsonOne, returnMSG, "", "", gsUrl, String.valueOf(LocalDateTime.now()));
                if (loginsert > 0) {
                    //System.out.println("日志写入成功！！");
                } else {
                    System.out.println("日志写入失败！！");
                }

            } else if (returnObj.get("code").equals("1")) { //失败
                jsonOne = jsonOne.replace("'", "\\\\''");
                returnMSG = returnMSG.replace("'", "\\\\''");
                //写入日志表
                int loginsert = loginfo.loginfo(jdbcTemplate, methodName, LocalDateTime.now().toString(), "失败", jsonOne, returnMSG, "", "", gsUrl, String.valueOf(LocalDateTime.now()));
                if (loginsert > 0) {
                    //System.out.println("日志写入成功！！");
                } else {
                    System.out.println("日志写入失败！！");
                }
            } else if (returnObj.get("code") == null && ("").equals(returnObj.get("code"))) {
                jsonOne = jsonOne.replace("'", "\\\\''");
                returnMSG = returnMSG.replace("'", "\\\\''");
                //写入日志表
                int loginsert = loginfo.loginfo(jdbcTemplate, methodName, LocalDateTime.now().toString(), "数据返回异常", jsonOne, returnMSG, "", "", gsUrl, String.valueOf(LocalDateTime.now()));
                if (loginsert > 0) {
                    //System.out.println("日志写入成功！！");
                } else {
                    System.out.println("日志写入失败！！");
                }
            }
        }
        return Result.success(returnMSG);
    }

    /**
     * (三)文件处理状态
     */

    @PostMapping("/fileProcessingStatus")
    public Result fileProcessingStatus() {
        String methodName = "文件处理状态接口";
        //er_bxzb  报销单

        //获取报文，后续需要手动报文拼接
        String jsonOne = "{\n" +
                "\t\"ORGCODE\": \"1001\",\n" +
                "\t\"BILLID\": [\"单据UUID\", \"123123\"]\n" +
                "\t\"ZIPCODE\": \"10012024120101\",\n" +
                "\t\"PUSHSTARTTIME\": \"2023-01-01\",\n" +
                "\t\"PUSHENDTIME\": \"2024-01-01\"\n" +
                "}";
        //调用GS接口，推送给GS浪潮，获取返回值;
        StopWatch st = new StopWatch();
        st.start();
        String gsUrl = IP + ":" + PORT + "/api/jg/syncbddata/v1.0/bdsyncinterface/syncWldwList";
        String returnMSG = HttpRequest.post(gsUrl)
                .header("Content-Type", "application/json")
                .header("Accept-Language", "zh-CHS")
                .header("X-ECC-Current-Tenant", ECC)
                .header("Authorization", Bearer).timeout(200000)
                .body(jsonOne).execute().body();
        st.stop();
        System.out.println("结束时间：" + LocalDateTime.now());
        System.out.println("接口耗时：" + st.getTotalTimeMillis() + "ms");
        System.out.println("接口返回值：" + returnMSG);
        if (!returnMSG.isEmpty()) {
            JSONObject returnObj = JSONObject.parseObject(returnMSG);
            if (returnObj.get("code").equals("0")) {//成功
                //写入日志表
                int loginsert = loginfo.loginfo(jdbcTemplate, methodName, LocalDateTime.now().toString(), "成功", jsonOne, returnMSG, "", "", gsUrl, String.valueOf(LocalDateTime.now()));
                if (loginsert > 0) {
                    //System.out.println("日志写入成功！！");
                } else {
                    System.out.println("日志写入失败！！");
                }

            } else if (returnObj.get("code").equals("1")) { //失败
                jsonOne = jsonOne.replace("'", "\\\\''");
                returnMSG = returnMSG.replace("'", "\\\\''");
                //写入日志表
                int loginsert = loginfo.loginfo(jdbcTemplate, methodName, LocalDateTime.now().toString(), "失败", jsonOne, returnMSG, "", "", gsUrl, String.valueOf(LocalDateTime.now()));
                if (loginsert > 0) {
                    //System.out.println("日志写入成功！！");
                } else {
                    System.out.println("日志写入失败！！");
                }
            } else if (returnObj.get("code") == null && ("").equals(returnObj.get("code"))) {
                jsonOne = jsonOne.replace("'", "\\\\''");
                returnMSG = returnMSG.replace("'", "\\\\''");
                //写入日志表
                int loginsert = loginfo.loginfo(jdbcTemplate, methodName, LocalDateTime.now().toString(), "数据返回异常", jsonOne, returnMSG, "", "", gsUrl, String.valueOf(LocalDateTime.now()));
                if (loginsert > 0) {
                    //System.out.println("日志写入成功！！");
                } else {
                    System.out.println("日志写入失败！！");
                }
            }
        }
        return Result.success(returnMSG);
    }


    /**
     * (一)文件影像描述信息接口 json报文
     *
     * @return
     */
    private String jsonOne() {
        return "{\n" +
                "    \"PACAGECODE\": \"1001202412020001\",\n" +
                "    \"PACAGESIZE\": 230,\n" +
                "    \"DWBH\": \"001\",\n" +
                "    \"DJLX\": \"PZ\",\n" +
                "    \"YEAR\": \"2024\",\n" +
                "    \"ZIPINFOS\": [\n" +
                "        {\n" +
                "            \"BILLID\": \"CLBX20230001\",\n" +
                "            \"SRCBILLID\": \"原始单据ID\",\n" +
                "            \"FILECOUNT\": 1,\n" +
                "            \"FILEINFOS\": [\n" +
                "                {\n" +
                "                    \"ID\": \"1971cb51-dc33-43e2-b242-830ed01a4030\",\n" +
                "                    \"FileName\": \"网络及信息安全承诺书.docx\",\n" +
                "                    \"State\": \"\",\n" +
                "                    \"BillID\": \"ffd7de7d-3212-44dd-b5f6-4225e4bcf5dc\",\n" +
                "                    \"BillCode\": \"%E6%A0%87%E5%87%86%E9%87%87%E8%B4%AD%E7%94%B3%E8%AF%B7\",\n" +
                "                    \"FileType\": \"\",\n" +
                "                    \"FileOrd\": 0,\n" +
                "                    \"FileCode\": \"\",\n" +
                "                    \"FileLength\": 21691,\n" +
                "                    \"FilePath\": \"/SYS/2024/202411/20241128/1971cb51-dc33-43e2-b242-830ed01a4030.docx\",\n" +
                "                    \"AppCode\": \"SYS\",\n" +
                "                    \"AppID\": \"SYS\",\n" +
                "                    \"FileDegree\": 0,\n" +
                "                    \"IsEncrpty\": \"0\",\n" +
                "                    \"EXT\": \".docx\",\n" +
                "                    \"TAG\": \"\",\n" +
                "                    \"IsCutImg\": \"\",\n" +
                "                    \"CreateTime\": \"2024-11-28 20:23:39\",\n" +
                "                    \"CreateUser\": \"fe21eebf-f20f-47ab-9d9d-144a01996022\",\n" +
                "                    \"ModifyUser\": \"fe21eebf-f20f-47ab-9d9d-144a01996022\",\n" +
                "                    \"ModifyTime\": \"2024-11-28 20:23:39\",\n" +
                "                    \"SecSystemId\": \"\",\n" +
                "                    \"SecLevel\": \"\",\n" +
                "                    \"dwbh\": \"\",\n" +
                "                    \"BillState\": \"\",\n" +
                "                    \"ThumFileId\": \"\",\n" +
                "                    \"RootId\": \"$\",\n" +
                "                    \"BillCATEGORY\": \"\",\n" +
                "                    \"BillTypeID\": \"\",\n" +
                "                    \"SourceSys\": \"\",\n" +
                "                    \"BillType\": \"\",\n" +
                "                    \"fileMd5\": \"2546a7524ce026d5adfb91b97671f71a\",\n" +
                "                    \"TypeTag\": \"\",\n" +
                "                    \"orientation\": 0,\n" +
                "                    \"spare1\": \"\",\n" +
                "                    \"spare2\": \"\",\n" +
                "                    \"spare3\": \"\",\n" +
                "                    \"spare4\": \"\",\n" +
                "                    \"spare5\": \"\",\n" +
                "                    \"invoiceCount\": 0,\n" +
                "                    \"invoiceZJE\": 0.0,\n" +
                "                    \"ocrcount\": 0,\n" +
                "                    \"uploadtype\": \"LocalUpload\",\n" +
                "                    \"ordNum\": 0,\n" +
                "                    \"manageInfo\": \"1\",\n" +
                "                    \"encryptExtend\": \"0\"\n" +
                "                },\n" +
                "                {}\n" +
                "            ]\n" +
                "        },\n" +
                "        {\n" +
                "            \"BILLID\": \"CLBX20230002\",\n" +
                "            \"SRCBILLID\": \"原始单据ID\",\n" +
                "            \"FILECOUNT\": 2,\n" +
                "            \"FILEINFOS\": [\n" +
                "                {\n" +
                "                    \"ID\": \"1971cb51-dc33-43e2-b242-830ed01a4030\",\n" +
                "                    \"FileName\": \"网络及信息安全承诺书.docx\",\n" +
                "                    \"State\": \"\",\n" +
                "                    \"BillID\": \"ffd7de7d-3212-44dd-b5f6-4225e4bcf5dc\",\n" +
                "                    \"BillCode\": \"%E6%A0%87%E5%87%86%E9%87%87%E8%B4%AD%E7%94%B3%E8%AF%B7\",\n" +
                "                    \"FileType\": \"\",\n" +
                "                    \"FileOrd\": 0,\n" +
                "                    \"FileCode\": \"\",\n" +
                "                    \"FileLength\": 21691,\n" +
                "                    \"FilePath\": \"/SYS/2024/202411/20241128/1971cb51-dc33-43e2-b242-830ed01a4030.docx\",\n" +
                "                    \"AppCode\": \"SYS\",\n" +
                "                    \"AppID\": \"SYS\",\n" +
                "                    \"FileDegree\": 0,\n" +
                "                    \"IsEncrpty\": \"0\",\n" +
                "                    \"EXT\": \".docx\",\n" +
                "                    \"TAG\": \"\",\n" +
                "                    \"IsCutImg\": \"\",\n" +
                "                    \"CreateTime\": \"2024-11-28 20:23:39\",\n" +
                "                    \"CreateUser\": \"fe21eebf-f20f-47ab-9d9d-144a01996022\",\n" +
                "                    \"ModifyUser\": \"fe21eebf-f20f-47ab-9d9d-144a01996022\",\n" +
                "                    \"ModifyTime\": \"2024-11-28 20:23:39\",\n" +
                "                    \"SecSystemId\": \"\",\n" +
                "                    \"SecLevel\": \"\",\n" +
                "                    \"dwbh\": \"\",\n" +
                "                    \"BillState\": \"\",\n" +
                "                    \"ThumFileId\": \"\",\n" +
                "                    \"RootId\": \"$\",\n" +
                "                    \"BillCATEGORY\": \"\",\n" +
                "                    \"BillTypeID\": \"\",\n" +
                "                    \"SourceSys\": \"\",\n" +
                "                    \"BillType\": \"\",\n" +
                "                    \"fileMd5\": \"2546a7524ce026d5adfb91b97671f71a\",\n" +
                "                    \"TypeTag\": \"\",\n" +
                "                    \"orientation\": 0,\n" +
                "                    \"spare1\": \"\",\n" +
                "                    \"spare2\": \"\",\n" +
                "                    \"spare3\": \"\",\n" +
                "                    \"spare4\": \"\",\n" +
                "                    \"spare5\": \"\",\n" +
                "                    \"invoiceCount\": 0,\n" +
                "                    \"invoiceZJE\": 0.0,\n" +
                "                    \"ocrcount\": 0,\n" +
                "                    \"uploadtype\": \"LocalUpload\",\n" +
                "                    \"ordNum\": 0,\n" +
                "                    \"manageInfo\": \"1\",\n" +
                "                    \"encryptExtend\": \"0\"\n" +
                "                }\n" +
                "            ]\n" +
                "        }\n" +
                "    ]\n" +
                "}";
    }
}
